
/**
 * 
 */
package vehicleLogic;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import constants.Constants;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import validator.Validator;
import vehicleEntity.PurchasedVehicleEntity;
import vehicleEntity.RentalVehicleEntity;
import vehicleEntity.SaleVehicleEntity;
import vehicleService.MoveService;

/**
 * @author Yokesh Kumar
 *
 */
public class MoveLogic {

	/**
	 * This method retrieves the list of field values
	 * @param from
	 * @Pre The String from value is retrieved from the MoveVehicleController class
	 * @Post Values are set to the table columns fields
	 * @return
	 */
	public ObservableList<PurchasedVehicleEntity> fetchVehicles(String from){
		ResultSet rs = null;
		MoveService service = new MoveService();
		ObservableList<PurchasedVehicleEntity> observableSaleList = null;
		List<PurchasedVehicleEntity> rentList = new ArrayList<PurchasedVehicleEntity>();
		PurchasedVehicleEntity purchase = null;
		try {
			if(from.equalsIgnoreCase(Constants.PURCHASE)){
				rs = service.fetchPurchasedvehicles();
			}
			else if(from.equalsIgnoreCase(Constants.RENT)){
				rs = service.searchOldRentVehicles(Constants.VEHICLES_OLD);
			}
			else if(from.equalsIgnoreCase(Constants.SALE)){
				rs = service.fetchSaleVehicles();
			}
			if(rs != null){
				while(rs.next()){
					purchase = new PurchasedVehicleEntity();
					purchase.setVehicleId(rs.getInt("vehicleId"));
					purchase.setPurchasedDate(rs.getDate("purchasedDate"));
					purchase.setManufDate(rs.getDate("manufDate"));
					purchase.setAmount(rs.getDouble("amount"));
					purchase.setRegNo(rs.getString("RegNo"));
					purchase.setVehicleType(rs.getString("vehicleType"));
					purchase.setVehicleName(rs.getString("vehicleName"));
					rentList.add(purchase);
				}
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		if(rentList != null){
			Calendar cal = Calendar.getInstance();
			int currYear = cal.get(Calendar.YEAR);
			
			for(PurchasedVehicleEntity purchaseEntity: rentList){
				purchaseEntity.setAmount(Validator.formatDouble(purchaseEntity.getAmount()));
				cal.setTime(purchaseEntity.getManufDate());
				int year = currYear - cal.get(Calendar.YEAR);
				purchaseEntity.setYearsOld(year);
			}
		}
		
		observableSaleList = FXCollections.observableList(rentList);
		service.closeConnection();
		return observableSaleList;
	}
	
	/**
	 * this method searches for the vehicle in the purchasedVehicle table
	 * @param vehicleId
	 * @Pre VehicleID and from values are retrieved from the MoveVehicleController class
	 * @Post The Arraylist valus are then set to the corresponding table field column values
	 * @return none
	 */
	public ObservableList<PurchasedVehicleEntity> searchVehicle(int vehicleId, String from){
		ResultSet rs = null;
		MoveService service = new MoveService();
		ObservableList<PurchasedVehicleEntity> observableSaleList = null;
		List<PurchasedVehicleEntity> rentList = new ArrayList<PurchasedVehicleEntity>();
		PurchasedVehicleEntity purchase = null;
		try {
			if(from.equalsIgnoreCase(Constants.PURCHASE)){
				rs = service.searchPurchase(vehicleId);
			}
			else if(from.equalsIgnoreCase(Constants.RENT)){
				rs = service.searchRent(vehicleId);
			}
			else if(from.equalsIgnoreCase(Constants.SALE)){
				rs = service.searchSale(vehicleId);
			}
			if(rs != null){
				while(rs.next()){
					purchase = new PurchasedVehicleEntity();
					purchase.setVehicleId(rs.getInt("vehicleId"));
					purchase.setPurchasedDate(rs.getDate("purchasedDate"));
					purchase.setManufDate(rs.getDate("manufDate"));
					purchase.setAmount(rs.getDouble("amount"));
					purchase.setRegNo(rs.getString("RegNo"));
					purchase.setVehicleType(rs.getString("vehicleType"));
					purchase.setVehicleName(rs.getString("vehicleName"));
					rentList.add(purchase);
				}
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		if(rentList != null){
			Calendar cal = Calendar.getInstance();
			int currYear = cal.get(Calendar.YEAR);
			
			for(PurchasedVehicleEntity purchaseEntity: rentList){
				purchaseEntity.setAmount(Validator.formatDouble(purchaseEntity.getAmount()));
				cal.setTime(purchaseEntity.getManufDate());
				int year = currYear - cal.get(Calendar.YEAR);
				purchaseEntity.setYearsOld(year);
			}
		}
		
		observableSaleList = FXCollections.observableList(rentList);
		service.closeConnection();
		return observableSaleList;
	}
	
	/**
	 * This method moves the vehicle with vehicle ID from purchase status 
	 * to Rent status
	 * @param vehicleId
	 * @Pre Arraylist of vehicle list table column values to be set are retrieved from the MoveVehicleController class
	 * @Post Arraylist values are set to the corresponding vehicle entity variables
	 * @return none
	 */
	
	public String movePurchasetoRent(ObservableList<PurchasedVehicleEntity> vehicleList){
		RentalVehicleEntity rentalEntity = null;
		MoveService service = new MoveService();
		if(vehicleList.size() >0){
			for(PurchasedVehicleEntity vehicleEntity : vehicleList){
				rentalEntity = new RentalVehicleEntity();
				System.out.println(vehicleEntity.getVehicleId());
				rentalEntity.setVehicleId(vehicleEntity.getVehicleId());
				rentalEntity.setStatus(Constants.VEHICLE_AVAILABLE);
				try {
					service.movePurchasetoRent(rentalEntity);
				} catch (SQLException e) {
					e.printStackTrace();
					return "Moving vehicle failed";
				}
			}
			service.closeConnection();
			return "Vehicle moved successfully";
		}
		return "Please select the vehicle to be moved";
	}
	
	/**
	 * this method searches for the vehicle in the SaleVehicle table
	 * @param vehicleId
	 * @return
	 */
	public SaleVehicleEntity searchSale(int vehicleId){
		
		return null;
	}
	
	/**
	 * This method moves the vehicle with vehicle ID from sale status 
	 * to Rent status
	 * @param vehicleId
	 * @Pre vehicleId values are retrieved from the MoveVehicleController class
	 * @Post RentalVehicleEntity variable values are passed to the MoveService class
	 * @return none
	 */
	
	public String moveSaleToRent(ObservableList<PurchasedVehicleEntity> vehicleList){
		RentalVehicleEntity rentEntity = null;
		MoveService service = new MoveService();
		if(vehicleList.size() >0){
			for(PurchasedVehicleEntity vehicleEntity : vehicleList){
				rentEntity = new RentalVehicleEntity();
				rentEntity.setVehicleId(vehicleEntity.getVehicleId());
				rentEntity.setStatus(Constants.VEHICLE_AVAILABLE);;
				try {
					service.moveSaleToRent(rentEntity);
					
				} catch (SQLException e) {
					e.printStackTrace();
					return "Moving vehicle failed";
				}
			}
			service.closeConnection();
			return "Vehicle moved successfully";
		}
		return "Please select the vehicle to be moved";
	}
	

	/**
	 * This method moves the vehicle with vehicle ID from Rent status 
	 * to sale status
	 * @param vehicleId
	 * @Pre The List of vehicleList values are retrieved from the MoveVehicleController class
	 * @Post The entity class variable values are passed to move service class to retrieve values
	 * @return none
	 */
	
	public String moveRentToSale(ObservableList<PurchasedVehicleEntity> vehicleList){
		SaleVehicleEntity saleEntity = null;
		double saleAmount = 0.0;
		MoveService service = new MoveService();
		if(vehicleList.size() >0){
			for(PurchasedVehicleEntity vehicleEntity : vehicleList){
				saleEntity = new SaleVehicleEntity();
				saleEntity.setVehicleId(vehicleEntity.getVehicleId());
				if(vehicleEntity.getYearsOld() > 10){
					saleAmount = (vehicleEntity.getAmount()/100)*Constants.GREATER_THAN_10_YEARS;
				}
				else if(vehicleEntity.getYearsOld() <= 10 && vehicleEntity.getYearsOld() <= 5){
					saleAmount = (vehicleEntity.getAmount()/100)*Constants.FIVE_TO_10_YEARS;
				}
				else if(vehicleEntity.getYearsOld() < 5){
					saleAmount = (vehicleEntity.getAmount()/100)*Constants.LESSER_THAN_5_YEARS;
				}
				
				saleEntity.setSaleAmount(Validator.formatDouble(saleAmount));
				try {
					service.moveRentToSale(saleEntity);
				} catch (SQLException e) {
					e.printStackTrace();
					return "Moving vehicle failed";
				}
			}
			service.closeConnection();
			return "Vehicle moved successfully";
		}
		return "Please select the vehicle to be moved";
	}


	
}
